package com.cc.main.cmd;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.cc.main.Const;
import com.cc.main.ICommonCmd;
import com.cc.main.problems.bean.ProblemBean;
import com.cc.main.util.MyUtils;

/**
 * path设置指令
 * @author TR
 *
 */
public class PathCmd implements ICommonCmd{

	private String path = "";
	private List<String> list = new ArrayList<>();
	@Override
	public void execute(Object action) {
		// TODO Auto-generated method stub
		String params = (String)action;
		if(params.isEmpty()) {
			System.out.println("请输入日志路径。");
		}
		while(true) {
			String input = params;
			if(params.isEmpty()) {
				input = MyUtils.getInput(Const.MPathCmd);
				if(input.isEmpty()) {
					System.out.println("输入错误,路径不能为空.");
					continue;
				}
				if(input.equalsIgnoreCase("quit")) {
					break;
				}
				if(input.equalsIgnoreCase("ls")) {
					for(String ll:list) {
						System.out.println(ll);
					}
					continue;
				}
			}
			
			path = input;
			list.clear();
			List<String> fileList = MyUtils.getFileList(true,path);
			System.out.println("--------------------------------------");
			
			if(fileList.isEmpty()) {//未找到panel.log开头 + 时间格式的日志log
				fileList = MyUtils.getFileList(false,path);
				list.addAll(fileList);
				System.out.println("目录下未找到.log结尾的日志文件.但发现其他文件的日志文件，数量:" + fileList.size() + "\n");
				for(String name:fileList) {
					System.out.println(name);
				}
			}else {//找到了日志panel.log开头 + 时间格式的日志log
				System.out.println("目录下发现.log结尾的日志文件,数量:" + fileList.size() + "\n");
				Collections.sort(fileList);
				//打印文件列表
				for(String timeStr:fileList) {
					System.out.println(timeStr);
					list.add(timeStr);
				}
			}
			break;
		}
		ProblemBean.getInstance().logFileList.clear();
		ProblemBean.getInstance().logFileList.addAll(list);
	}

	@Override
	public String printCurrentEnv() {
		// TODO Auto-generated method stub
		return Const.MPathCmd + ":" + path;
	}
	
	/**
	 * 按时间排列
	 * @param list
	 * @return
	 */
//	private ArrayList<String> sortByTime(List<String> list){
//		ArrayList<String> slist = new ArrayList<String>();
//		for(String item :list) {
//			//截取panel.log.01_2022-06-24-16-30-53-000.log 的2022-06-24-16-30-53-000.log内容
//			slist.add(item.substring(item.length()-27));
//		}
//		Collections.sort(slist);
//		return slist;
//	}

}
